package com.baseDemo.web.service.uploadimage.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baseDemo.web.dao.uploadimage.UploadImageEntityDao;
import com.baseDemo.web.entity.UploadImageEntity;
import com.baseDemo.web.service.uploadimage.UploadImageService;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;

@Service
public class UploadImageServiceImpl extends
		BaseServiceSupport<UploadImageEntity> implements UploadImageService {

	@Resource
	UploadImageEntityDao uploadImageEntityDao;

	@Override
	public String upload(UploadImageEntity uploadImageEntity) {
		UploadImageEntity temp = uploadImageEntityDao.get(uploadImageEntity.getOwnerEntity(), uploadImageEntity.getOwnerId());
		CommonsMultipartFile multipartFile = uploadImageEntity.getMultipartFile();
		if(temp!=null){
			temp.setFileName(multipartFile.getOriginalFilename());
			temp.setImageFile(multipartFile.getBytes());
			temp.setFileType(multipartFile.getContentType());
			return "{'success':'"+uploadImageEntityDao.update(temp)+"'}";
		}else{
			uploadImageEntity.setFileName(multipartFile.getOriginalFilename());
			uploadImageEntity.setImageFile(multipartFile.getBytes());
			uploadImageEntity.setFileType(multipartFile.getContentType());
			return "{'success':'"+uploadImageEntityDao.save(uploadImageEntity)+"'}";
		}
	}

	@Override
	public UploadImageEntity getImage(String ownerEntity, String ownerId) {
		return uploadImageEntityDao.get(ownerEntity, ownerId);
	}

}
